package com.cyms.modules.base.service.impl;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Map;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.cyms.common.utils.PageUtils;
import com.cyms.common.utils.Query;
import com.cyms.modules.area.dao.SysAreaDao;
import com.cyms.modules.area.entity.SysAreaEntity;
import com.cyms.modules.base.dao.CustomerDao;
import com.cyms.modules.base.entity.CustomerEntity;
import com.cyms.modules.base.service.CustomerService;


@Service("customerService")
public class CustomerServiceImpl extends ServiceImpl<CustomerDao, CustomerEntity> implements CustomerService {
	
	@Autowired
	private SysAreaDao areaDao;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
    	String code=(String) params.get("code");
    	String name=(String) params.get("name");
    	String personName=(String) params.get("personName");
    	String linkName=(String) params.get("linkName");
    	Long createOrg = (Long) params.get("createOrg");
        Page<CustomerEntity> page = this.selectPage(
                new Query<CustomerEntity>(params).getPage(),
                new EntityWrapper<CustomerEntity>()
						.eq(createOrg != null,"create_org",createOrg)
						.orderBy("create_date", false)
						.like(StringUtils.isNotBlank(code), "code", code)
						.like(StringUtils.isNotBlank(name), "name", name)
						.like(StringUtils.isNotBlank(personName), "person_name", personName)
						.like(StringUtils.isNotBlank(linkName), "link_name", linkName)
        );
        for (CustomerEntity obj : page.getRecords()) {        	
			SysAreaEntity area = areaDao.selectById(obj.getArea());
			if (area != null) {
				obj.setArea(area.getTreeNames());
			}
		}
        
        return new PageUtils(page);
    }

}
